
# encoding=utf-8
# syntax类型定义：1-定义变量,2-执行替换,3-宏定义
def read_syntaxs_from_tokens(tokens, path):
    syntaxs = []
    state = 0
    tmp = {}
    tmpcont = {}
    for token in tokens:
        if token['token_type'] == 7:
            if state == 6:
                tmp['content'].append({'type': 1, 'cont': token['content']})
            elif state == 11:
                tmp['content'].append({'type': 3, 'cont': token['content']})
            continue
        if state == 0:
            if token['token_type'] == 1:
                state = 1
                tmp['syn_type'] = 1
                tmp['name'] = ''
                tmp['params'] = []
                tmp['content'] = []
            elif token['token_type'] == 3:
                state = 2
                tmp['syn_type'] = 2
                tmp['name'] = ''
                tmp['params'] = []
                tmp['file'] = ''
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 1:
            if token['token_type'] == 2:
                state = 3
            elif token['token_type'] == 6:
                if tmp['name'] == '':
                    tmp['name'] = token['content']
                else:
                    tmp['params'].append(token['content'])
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 2:
            if token['token_type'] == 4:
                state = 0
            elif token['token_type'] == 8:
                state = 7
            elif token['token_type'] == 6:
                if tmp['name'] == '':
                    tmp['name'] = token['content']
                else:
                    tmp['params'].append(token['content'])
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 3:
            if token['token_type'] == 5:
                state = 5
            elif token['token_type'] == 9:
                state = 10
                tmp['syn_type'] = 3
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 5:
            if token['token_type'] == 1:
                state = 6
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 6:
            if token['token_type'] == 2:
                state = 0
            elif token['token_type'] == 3:
                state = 9
                tmpcont['name'] = ''
                tmpcont['params'] = []
            else:
                tmp['content'].append({'type': 1, 'cont': token['content']})
        elif state == 7:
            if token['token_type'] == 6:
                state = 8
                tmp['file'] = token['content']
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 8:
            if token['token_type'] == 4:
                state = 0
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 9:
            if token['token_type'] == 4:
                state = 6
                tmp['content'].append({'type': 2, 'cont': tmpcont})
                tmpcont = {}
            elif token['token_type'] == 6:
                if tmpcont['name'] == '':
                    tmpcont['name'] = token['content']
                else:
                    tmpcont['params'].append(token['content'])
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 10:
            if token['token_type'] == 1:
                state = 11
            else:
                print('invalid input at ' + path + ' in line: ' + token['line'] + '   ' + token['content'])
                exit()
        elif state == 11:
            if token['token_type'] == 2:
                state = 0
            else:
                tmp['content'].append({'type': 3, 'cont': token['content']})

        if state == 0:
            syntaxs.append(tmp)
            tmp = {}

    return syntaxs
